package com.yyh.controller;


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyh.entity.Follow;
import com.yyh.entity.Student;
import com.yyh.entity.StudentParam;
import com.yyh.entity.Users;
import com.yyh.entity.UsersParam;
import com.yyh.mapper.UsersMapper;
import com.yyh.service.IStudentService;
import com.yyh.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangyueheng
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	IStudentService iStudentService;
	
	/**
	 * 多条件分页查询
	 * @return
	 */
	@RequestMapping("/getList")
	public Object getList(StudentParam s) {
		//分页-需要分页的配置文件
		Page<Student> page = new Page<>(s.getPage(),s.getLimit());
		
		//多条件
		QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
		if(!StringUtils.isEmpty(s.getStuName())) {
			queryWrapper.like("stu_name", s.getStuName());
		}
		if(!StringUtils.isEmpty(s.getStartTime())) {
			queryWrapper.gt("create_time", s.getStartTime());
		}
		if(!StringUtils.isEmpty(s.getEndTime())) {
			queryWrapper.lt("create_time", s.getEndTime());
		}
		if(!StringUtils.isEmpty(s.getIsDelete())) {
			queryWrapper.eq("is_delete", s.getIsDelete());
		}
		
		IPage<Student> ipage = iStudentService.page(page, queryWrapper);
		
		List<Student> data = ipage.getRecords();
		long total = ipage.getTotal();
		return new Result(0,"",data,total);
	}
	
	/**
	 * 修改咨询师
	 * @param student
	 * @param stuId
	 * @return
	 */
	@PostMapping("/updateZxs")
	public Object updateZxs(@Valid Student student,@RequestParam Integer stuId) {
		student.setStuId(stuId);
		boolean res = iStudentService.updateById(student);
		if(res) {
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"修改失败");
		}
	}
	
	/**
	 * 删除（软删）
	 * @param stuId
	 * @return
	 */
	@PostMapping("/delete")
	public Object updateDelete(@RequestParam Integer stuId) {
		Student student = new Student();
		student.setStuId(stuId);
		student.setIsDelete(1);
		boolean res = iStudentService.updateById(student);
		if(res) {
			return new Result(0,"删除成功");
		}else {
			return new Result(1,"删除失败");
		}
	}
	
	/**
	 * 获取所有学生
	 * @param users
	 * @return
	 */
	@RequestMapping("/getStu")
    public  Object getStu(StudentParam s){
        List<Student> stu = iStudentService.getStu(s);
        return new Result(0,"学生列表",stu,(long) stu.size());
    }
	
	/**
	 * 获取咨询师的学生
	 * @param users
	 * @return
	 */
	@RequestMapping("/getZxsStu")
    public Object getZxsStu(StudentParam s,@RequestParam Integer zxsId){
		s.setZxsId(zxsId);
        List<StudentParam> zxs = iStudentService.getZxsStu(s);
        return new Result(0,"学生列表",zxs,(long) zxs.size());
	}
	
	/**
	 * 咨询师修改缴费状态
	 * @param student
	 * @param stuId
	 * @return
	 */
	@PostMapping("/updateZxsStu")
	public Object updateZxsStu(@Valid Student student,@RequestParam Integer stuId) {
		student.setStuId(stuId);
		boolean res = iStudentService.updateZxsStu(student);
		if(res) {
			return new Result(0,"缴费成功");
		}else {
			return new Result(1,"缴费失败");
		}
	}
	
	/**
	 * 获取网络咨询添加的学生
	 * @param users
	 * @return
	 */
	@RequestMapping("/getWlzxStu")
    public Object getWlzxStu(StudentParam s,@RequestParam Integer wlzxId){
		s.setWlzxId(wlzxId);
        List<StudentParam> wlzx = iStudentService.getWlzxStu(s);
        return new Result(0,"学生列表",wlzx,(long) wlzx.size());
	}
	
	/**
	 * 网络咨询师添加学生
	 * @param student
	 * @param wlzxId
	 * @return
	 */
	@PostMapping("/insertStu")
	public Object insertStu(Student student,Integer wlzxId) {
		student.setWlzxId(wlzxId);
		boolean res = iStudentService.insertStu(student);
		if(res) {
			return new Result(0,"添加成功");
		}else {
			return new Result(1,"添加失败");
		}
	}
	
	@PostMapping("/insertFollow")
	public Object insertFollow(Follow follow,Integer studentId) {
		follow.setStudentId(studentId);
		boolean res = iStudentService.insertFollow(follow);
		if(res) {
			return new Result(0,"提交成功");
		}else {
			return new Result(1,"提交失败");
		}
	}
}
